package com.fita.admin.web.controller.data;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.fita.admin.service.data.CountryService;
import com.fita.admin.service.data.DiscountService;
import com.fita.entity.data.Country;
import com.fita.entity.data.CountryCoupon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 优惠管理
 *
 * @author cuteHU
 *
 */
@Controller
@RequestMapping("/discounts")
@MenuSetting("menu-coupon")
public class DiscountController {

    private Logger logger = LoggerFactory.getLogger(DiscountController.class);

    @Autowired
    private DiscountService discountService;

    @Autowired
    private CountryService countryService;

    /**
     * list
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-country-coupon-management")
    public ModelAndView list(Page page) {

        ModelAndView modelAndView = new ModelAndView("data/discount/discount-list");

        int totals = discountService.count(CountryCoupon.class);
        if (totals > 0) {
            List<CountryCoupon> discounts = discountService.listByPage(CountryCoupon.class, page.with(totals));
            modelAndView.addObject("discounts", discounts);
            modelAndView.addObject("page", page);

        }
        return modelAndView;

    }

    /**
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    @MenuSetting("submenu-country-coupon-management")
    public ModelAndView create() {
        ModelAndView modelAndView = new ModelAndView("data/discount/discount-create");
        return modelAndView;
    }

    /**
     * @param vehicleParameter
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(CountryCoupon coupon, @RequestParam("countryCode") String countryCode) {

        HttpResult httpResult = new HttpResult();

        try {

            Country country = countryService.load(countryCode);
            discountService.createCoupon(coupon, country);
            httpResult.success().message("新增优惠管理成功");

        } catch (BusinessException e) {
            httpResult.fail(e.getCode()).message(e.getMessage());
            logger.error("onCreate occurs error: ", e);

        } catch (Exception e) {
            logger.error("onCreate occurs error: ", e);
            httpResult.fail("500").message("新增优惠管理失败");
        }

        return httpResult;
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("data/discount/discount-edit");

        CountryCoupon coupon = discountService.load(CountryCoupon.class, id);
        modelAndView.addObject("coupon", coupon);

        return modelAndView;
    }

    /**
     * @param vehicleParameter
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@Preloading("couponId") CountryCoupon coupon,
            @RequestParam("countryCode") String countryCode) {

        HttpResult httpResult = new HttpResult();
        try {

            Country country = countryService.load(countryCode);
            discountService.updateCoupon(coupon, country);
            httpResult.success().message("修改优惠管理信息成功");
        } catch (Exception e) {
            httpResult.fail("500").message("修改优惠管理失败");
            logger.error("#onEdit() - edit fail, exception is [{}]", e);
        }

        return httpResult;
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/activate", method = RequestMethod.GET)
    public ModelAndView activate(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("data/discount/discount-activate");
        modelAndView.addObject("id", id);
        return modelAndView;
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/activate", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onActivate(@PathVariable("id") Long id) {
        HttpResult result = new HttpResult();
        try {

            CountryCoupon coupon = discountService.load(CountryCoupon.class, id);

            discountService.activate(coupon);

            result.success().message("生效成功！");

        } catch (Exception e) {

            result.fail("500").message("生效失败！");
        }

        return result;
    }

    /**
     * @param id
     * @return
     */

    @RequestMapping(value = "/{id}/freeze", method = RequestMethod.GET)
    public ModelAndView freeze(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("data/discount/discount-freeze");
        modelAndView.addObject("id", id);
        return modelAndView;
    }

    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/freeze", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onFreeze(@PathVariable("id") Long id) {
        HttpResult result = new HttpResult();
        try {

            CountryCoupon coupon = discountService.load(CountryCoupon.class, id);

            discountService.freeze(coupon);

            result.success().message("失效成功！");

        } catch (Exception e) {

            result.fail("500").message("失效失败！");
        }
        return result;
    }

    /**
     * @param couponId
     * @return
     */
    @RequestMapping(value = "/{couponId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("couponId") Long couponId) {

        ModelAndView modelAndView = new ModelAndView("data/discount/discount-delete");

        modelAndView.addObject("couponId", couponId);

        return modelAndView;
    }

    /**
     * @param couponId
     * @return
     */
    @RequestMapping(value = "/{couponId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onDelete(@PathVariable("couponId") Long couponId) {

        HttpResult result = new HttpResult();

        try {

            discountService.deleteCoupon(couponId);

            result.success().message("删除优惠成功");

        } catch (Exception e) {

            logger.error("#onDeleteCoupon - ", e);

            result.fail("500").message("删除优惠失败");

        }

        return result;

    }

}
